package cn.iocoder.lw.module.core.service.project;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.module.core.controller.admin.project.vo.*;
import cn.iocoder.lw.module.core.convert.project.CoreProjectConvert;
import cn.iocoder.lw.module.core.dal.dataobject.device.CoreDeviceDO;
import cn.iocoder.lw.module.core.dal.dataobject.project.CoreProjectDO;
import cn.iocoder.lw.module.core.dal.mysql.project.CoreProjectMapper;
import cn.iocoder.lw.module.system.api.dept.DeptApi;
import cn.iocoder.lw.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.lw.module.system.api.user.AdminUserApi;
import cn.iocoder.lw.module.system.api.user.dto.AdminUserRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.lw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.lw.module.core.enums.ErrorCodeConstants.PROJECT_NOT_EXISTS;

/**
 * 核心项目 Service 实现类
 *
 * @author Lw源码
 */
@Service
@Slf4j
public class CoreProjectServiceImpl implements CoreProjectService {

    @Resource
    private CoreProjectMapper projectMapper;

    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private DeptApi deptApi;

    @Override
    public Long createProject(CoreProjectSaveReqVO createReqVO, Long userId) {
        // 插入
        CoreProjectDO project = CoreProjectConvert.INSTANCE.convert(createReqVO);
        projectMapper.insert(project);
        // 返回
        return project.getId();
    }

    @Override
    public void updateProject(CoreProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectExists1(updateReqVO.getId());
        // 更新
        CoreProjectDO updateObj = CoreProjectConvert.INSTANCE.convert(updateReqVO);
        projectMapper.updateById(updateObj);
    }

    @Override
    public void deleteProject(Long id) {
        // 校验存在
        validateProjectExists1(id);
        // 删除
        projectMapper.deleteById(id);
    }

    private CoreProjectDO validateProjectExists1(Long id) {
        if (id == null) {
            return null;
        }
        CoreProjectDO project = projectMapper.selectById(id);
        if (project == null) {
            throw exception(PROJECT_NOT_EXISTS);
        }
        return project;
    }

    @Override
    public CoreProjectRespVO getProject(Long id) {
        CoreProjectDO project = projectMapper.selectById(id);
        if (project == null) {
            return null;
        }

        CoreProjectRespVO respVO = CoreProjectConvert.INSTANCE.convert(project);
        // 填充负责人信息
        setProjectOwnerInfo(respVO);
        return respVO;
    }

    @Override
    public PageResult<CoreProjectRespVO> getProjectPage(CoreProjectPageReqVO pageReqVO) {
        PageResult<CoreProjectDO> pageResult = projectMapper.selectPage(pageReqVO);
        PageResult<CoreProjectRespVO> respPageResult = CoreProjectConvert.INSTANCE.convertPage(pageResult);
        return respPageResult;
    }

    @Override
    public PageResult<CoreProjectImportExcelVO> getProjectList(CoreProjectPageReqVO pageReqVO) {
        PageResult<CoreProjectDO> pageResult = projectMapper.selectPage(pageReqVO);
        return BeanUtils.toBean(pageResult, CoreProjectImportExcelVO.class);
    }

    @Override
    public void validateProjectExists(Long id) {
        validateProjectExists1(id);
    }

    @Override
    public List<CoreProjectRespVO> getProjectListByOwnerUserId(Long ownerUserId) {
        List<CoreProjectDO> projects = projectMapper.selectListByOwnerUserId(ownerUserId);
        List<CoreProjectRespVO> respVOs = CoreProjectConvert.INSTANCE.convertList(projects);
        return respVOs;
    }

    /**
     * 设置项目的负责人的信息
     *
     * @param project 项目
     */
    private void setProjectOwnerInfo(CoreProjectRespVO project) {
        if (project.getOwnerUserId() == null) {
            return;
        }
        AdminUserRespDTO user = adminUserApi.getUser(project.getOwnerUserId());
        if (user == null) {
            return;
        }
        project.setOwnerUserName(user.getNickname());
        if (user.getDeptId() != null) {
            DeptRespDTO dept = deptApi.getDept(user.getDeptId());
            if (dept != null) {
                project.setOwnerDeptName(dept.getName());
            }
        }
    }


    public CoreProjectDO getProjectRaw(Long id) {
        return projectMapper.selectById(id);
    }


}